#include <d3d9.h>
#include <d3dx9.h>
#include "Game.h"
#include "Constants.h"
#include "BufferCollection.h"
#include "Utility.h"
#include "math.h"

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

extern Minicraft::BufferCollection* Minicraft::BufferCollectionInstance;
extern Minicraft::World* Minicraft::testWorld;
extern long Minicraft::MouseMoveX = 0;
extern long Minicraft::MouseMoveY = 0;

// Contains game-specific update code.
void Minicraft::Update()
{
	Minicraft::UpdateInput();
}

// Updates the game based on input
void Minicraft::UpdateInput()
{
	Minicraft::UpdateMouseInput();
	Minicraft::UpdateKeyboardInput();
}

// Updates the game based on Mouse input
void Minicraft::UpdateMouseInput()
{
	// update the look position of the camera based on the last relative movement of the mouse
	if (Minicraft::MouseMoveX != 0)
	{
		// if moving right
		if(Minicraft::MouseMoveX > 0)
		{
			// if currenting looking in positive z
			if(Minicraft::camera->GetLookPosition()->z > Minicraft::camera->GetCameraPosition()->z)
			{
				// Move x in the positive direction
				Minicraft::camera->GetLookPosition()->x += Minicraft::Constants::Controls::LOOK_SPEED;
			}
			else
			{
				// Move x in the negative direction
				Minicraft::camera->GetLookPosition()->x -= Minicraft::Constants::Controls::LOOK_SPEED;
			}
			// if currenting looking in positive x
			if(Minicraft::camera->GetLookPosition()->x > Minicraft::camera->GetCameraPosition()->x)
			{
				// Move z in the negative direction
				Minicraft::camera->GetLookPosition()->z -= Minicraft::Constants::Controls::LOOK_SPEED;
			}
			else
			{
				// Move z in the positive direction
				Minicraft::camera->GetLookPosition()->z += Minicraft::Constants::Controls::LOOK_SPEED;
			}
		}
		else
		{
			// if currenting looking in positive z
			if(Minicraft::camera->GetLookPosition()->z > Minicraft::camera->GetCameraPosition()->z)
			{
				// Move x in the negative direction
				Minicraft::camera->GetLookPosition()->x -= Minicraft::Constants::Controls::LOOK_SPEED;
			}
			else
			{
				// Move x in the positive direction
				Minicraft::camera->GetLookPosition()->x += Minicraft::Constants::Controls::LOOK_SPEED;
			}
			// if currenting looking in positive x
			if(Minicraft::camera->GetLookPosition()->x > Minicraft::camera->GetCameraPosition()->x)
			{
				// Move z in the positive direction
				Minicraft::camera->GetLookPosition()->z += Minicraft::Constants::Controls::LOOK_SPEED;
			}
			else
			{
				// Move z in the negative direction
				Minicraft::camera->GetLookPosition()->z -= Minicraft::Constants::Controls::LOOK_SPEED;
			}
		}
		Minicraft::MouseMoveX = 0;
	}
	// Updates the y look position
	if (Minicraft::MouseMoveY != 0)
	{	
		if(Minicraft::MouseMoveY > 0)
		{
			if(Minicraft::camera->GetLookPosition()->y != Minicraft::Constants::Camera::MAX_Y_LOOK)
			{
				Minicraft::camera->GetLookPosition()->y -= Minicraft::Constants::Controls::LOOK_SPEED;
			}
		}
		else
		{
			if(Minicraft::camera->GetLookPosition()->y != Minicraft::Constants::Camera::MIN_Y_LOOK)
			{
				Minicraft::camera->GetLookPosition()->y += Minicraft::Constants::Controls::LOOK_SPEED;
			}
		}
		Minicraft::MouseMoveY = 0;
	}
}

// Updates the game based on Keyboard input
void Minicraft::UpdateKeyboardInput()
{
	//If a movement key is held down, adjust the camera position and look position
	// Adjusts how much the camera should move in the x and z positions (based on where the camera is looking) and applies it to the camera
	// "W"
	if (KEY_DOWN(0x57))
	{
		float zMovement = (Minicraft::camera->GetLookPosition()->z - Minicraft::camera->GetCameraPosition()->z) / 10;
		float xMovement = (Minicraft::camera->GetLookPosition()->x - Minicraft::camera->GetCameraPosition()->x) / 10;

		Minicraft::camera->GetCameraPosition()->z += Minicraft::Constants::Controls::MOVE_SPEED * zMovement;
		Minicraft::camera->GetLookPosition()->z += Minicraft::Constants::Controls::MOVE_SPEED * zMovement;

		Minicraft::camera->GetCameraPosition()->x += Minicraft::Constants::Controls::MOVE_SPEED * xMovement;
		Minicraft::camera->GetLookPosition()->x += Minicraft::Constants::Controls::MOVE_SPEED * xMovement;
	}
	// "A"
	if (KEY_DOWN(0x41))
	{
		float xMovement = (Minicraft::camera->GetLookPosition()->z - Minicraft::camera->GetCameraPosition()->z) / 10;
		float zMovement = (Minicraft::camera->GetLookPosition()->x - Minicraft::camera->GetCameraPosition()->x) / 10;

		Minicraft::camera->GetCameraPosition()->z += Minicraft::Constants::Controls::MOVE_SPEED * zMovement;
		Minicraft::camera->GetLookPosition()->z += Minicraft::Constants::Controls::MOVE_SPEED * zMovement;

		Minicraft::camera->GetCameraPosition()->x -= Minicraft::Constants::Controls::MOVE_SPEED * xMovement;
		Minicraft::camera->GetLookPosition()->x -= Minicraft::Constants::Controls::MOVE_SPEED * xMovement;
	}
	// "S"
	if (KEY_DOWN(0x53))
	{
		float zMovement = (Minicraft::camera->GetLookPosition()->z - Minicraft::camera->GetCameraPosition()->z) / 10;
		float xMovement = (Minicraft::camera->GetLookPosition()->x - Minicraft::camera->GetCameraPosition()->x) / 10;

		Minicraft::camera->GetCameraPosition()->z -= Minicraft::Constants::Controls::MOVE_SPEED * zMovement;
		Minicraft::camera->GetLookPosition()->z -= Minicraft::Constants::Controls::MOVE_SPEED * zMovement;

		Minicraft::camera->GetCameraPosition()->x -= Minicraft::Constants::Controls::MOVE_SPEED * xMovement;
		Minicraft::camera->GetLookPosition()->x -= Minicraft::Constants::Controls::MOVE_SPEED * xMovement;
	}
	// "D"
	if (KEY_DOWN(0x44))
	{
		float xMovement = (Minicraft::camera->GetLookPosition()->z - Minicraft::camera->GetCameraPosition()->z) / 10;
		float zMovement = (Minicraft::camera->GetLookPosition()->x - Minicraft::camera->GetCameraPosition()->x) / 10;

		Minicraft::camera->GetCameraPosition()->z -= Minicraft::Constants::Controls::MOVE_SPEED * zMovement;
		Minicraft::camera->GetLookPosition()->z -= Minicraft::Constants::Controls::MOVE_SPEED * zMovement;

		Minicraft::camera->GetCameraPosition()->x += Minicraft::Constants::Controls::MOVE_SPEED * xMovement;
		Minicraft::camera->GetLookPosition()->x += Minicraft::Constants::Controls::MOVE_SPEED * xMovement;
	}
}